package com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class ReservationHeartConsultSendMessage extends ExecuteListener{

	public String getDescription() {
		return "预约心理咨询审批结束，给预约人发企业微信消息。";
	}

	@Override
	public void execute(ProcessExecutionContext param) throws Exception {
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(param.getTaskInstance(), "接单");
		if(flag_ok) {
			//获取流程实例ID
			String processInstId = param.getProcessInstance().getId();
			//根据流程实例ID获取任务表内数据
			List<Map<String , Object>> headlist = DBSql.query("SELECT * FROM BO_EU_RESERVATION_HEARTCONSULT  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			Map<String , Object>  jcbd = headlist.get(0);
			String WORKERNAME = CoreUtil.objToStr(jcbd.get("WORKERNAME"));//职工姓名
			String WORKERUNIT = CoreUtil.objToStr(jcbd.get("WORKERUNIT"));//职工单位
			String PHONE = CoreUtil.objToStr(jcbd.get("PHONE"));//联系电话
			String CONSULTTYPE = CoreUtil.objToStr(jcbd.get("CONSULTTYPE"));//咨询方向
			String REQUIRE = CoreUtil.objToStr(jcbd.get("REQUIRE"));//特殊要求
			String CLTIME = CoreUtil.objToStr(jcbd.get("CLTIME"));//预约时间
			String TIMESLOT = CoreUtil.objToStr(jcbd.get("TIMESLOT"));//预约时段
			String WORKERID = CoreUtil.objToStr(jcbd.get("WORKERID"));//职工账号
			if(!"".equals(WORKERUNIT) && !"".equals(WORKERID) ) {
				String sqluserId ="select EXT5  from ORGUSER  where USERID='"+WORKERID+"'";
		        String weixinid =CoreUtil.objToStr(DBSql.getString(sqluserId,"EXT5"));
		        String sql = "select t.*  from BO_EU_SHSY_NOTICE_UNITLIST t  where t.unit ='"+WORKERUNIT+"'";
		        List<Map<String, Object>> jbData = DBSql.query(sql, new ColumnMapRowMapper(), new Object[] {});
		        if (jbData.size() > 0 && !"".equals(weixinid)) {
		        	String agentid = CoreUtil.objToStr(jbData.get(0).get("AGENTID"));//应用id
					String corpid = CoreUtil.objToStr(jbData.get(0).get("CORPID"));// 企业微信单位id
					String agentsecretid = CoreUtil.objToStr(jbData.get(0).get("AGENTSECRETID"));//应用secretid
					String text="您好！您已预约"+CLTIME+" "+TIMESLOT+"心理关爱服务。"
							+ "地址：中春路1777号闵行航天城卫生所内心灵驿站（动力所一楼东侧），如有问题请联系021-24180833。";
					String acessToken = weComSendAnnouncementUtil.getAccessToken(corpid,agentsecretid);
					weComSendAnnouncementUtil.sendWeChatMessage(weixinid,"","text","",text,0,agentid,acessToken);
		        }
			}
			
		}
		
	}

}
